// pages/device-map/index.js
const api = require('../../utils/api.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: 23.099994,
    longitude: 113.324520,
    markers: [],
    show: false,
    id: null,
    devices: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  getDeviceList: function() {
    var that = this;
    wx.showLoading({
      title: '搜索设备中',
    })
    wx.requestWithCookie({
      url: api.getMachinesUrl,
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        MerchantCode: app.globalData.userInfo.Agents,
        pageIndex: 0,
        pageSize: 20,
        CoordinateType: 1,
        Longitude: that.data.longitude,
        Dimension: that.data.latitude,
        Range: 100000000
      },
      dataType: 'json',
      success: function (res) {
        wx.hideLoading()
        if (res.data.Code === 1) {
          var devices = res.data.Entity, markers = [];
          that.setData({
            devices: devices
          })
          for (var i = 0; i < devices.length; ++i) {
            var item = devices[i];
            markers.push({
              iconPath: '/images/location.png', id: item.Id, title: item.MachineAddress, latitude: item.Dimension, longitude: item.Longitude, width: 30, height: 30 })
          }
          that.setData({
            markers: markers
          })
        }
      }
    })
  },
  markertap(e) {
    // wx.navigateTo({
    //   url: '/pages/device-detail/index?cash_code=' + e.markerId
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        that.getDeviceList()
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})